<!DOCTYPE html>
<html>
<head>
<title>Electromagnetic Field Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="650" height="400" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Style settings ---
    ctx.lineWidth = 1.2;
    ctx.font = '16px Arial';
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';

    // --- Coordinates and dimensions ---
    const canvasWidth = canvas.width;
    const canvasHeight = canvas.height;
    const centerY = canvasHeight / 2;
    const fieldLineTop = 50;
    const fieldLineBottom = 350;
    const sphereRadius = 28;
    const sphereX = 180;
    const horizontalLineStartX = 40;
    const horizontalLineEndX = 500;

    // --- Draw Sphere ---
    ctx.beginPath();
    ctx.arc(sphereX, centerY, sphereRadius, 0, 2 * Math.PI);
    ctx.stroke();

    // --- Draw Plane of Motion (dashed line) ---
    ctx.setLineDash([7, 7]);
    // Part 1: from left edge to the sphere
    ctx.beginPath();
    ctx.moveTo(horizontalLineStartX, centerY);
    ctx.lineTo(sphereX - sphereRadius, centerY);
    ctx.stroke();
    // Part 2: from the sphere to the right end
    ctx.beginPath();
    ctx.moveTo(sphereX + sphereRadius, centerY);
    ctx.lineTo(horizontalLineEndX, centerY);
    ctx.stroke();
    ctx.setLineDash([]); // Reset to solid line

    // --- Draw Electric Field Lines ---
    const fieldLineXPositions = [120, 240, 360, 480];
    const arrowHeight = 10;
    const arrowWidth = 6;

    fieldLineXPositions.forEach(x => {
        // Draw the vertical line
        ctx.beginPath();
        ctx.moveTo(x, fieldLineTop);
        ctx.lineTo(x, fieldLineBottom);
        ctx.stroke();

        // Draw the arrowhead (pointing up). The tip of the arrow is on the horizontal plane.
        const arrowTipY = centerY;
        ctx.beginPath();
        ctx.moveTo(x - arrowWidth, arrowTipY + arrowHeight);
        ctx.lineTo(x, arrowTipY);
        ctx.lineTo(x + arrowWidth, arrowTipY + arrowHeight);
        ctx.stroke();
    });

    // --- Add Text Labels ---
    // Label: "electric field lines"
    ctx.textAlign = 'left';
    ctx.fillText('electric field lines', 370, 40);

    // Label: "sphere"
    ctx.textAlign = 'center';
    ctx.fillText('sphere', sphereX, centerY + sphereRadius + 20);

    // Label: "plane in which sphere moves"
    ctx.textAlign = 'left';
    const planeLabelX = horizontalLineEndX + 15;
    ctx.fillText('plane in which', planeLabelX, centerY - 10);
    ctx.fillText('sphere moves', planeLabelX, centerY + 10);

    // Label: "Fig. 2.1"
    ctx.textAlign = 'center';
    ctx.fillText('Fig. 2.1', canvasWidth / 2, canvasHeight - 25);

</script>
</body>
</html>